package com.example.locationreport.locationreport.utils;

import android.content.Context;
import android.util.Log;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * author : bq
 * date   : 2024/4/29
 * desc   : 补发点位数据所用到的工具
 */
public class ReissueUtils {

    private static final String TAG = ReissueUtils.class.getSimpleName();

    /**
     * 读取系统目录所有文件
     */
    public static ArrayList<String> getReadSystemDirectory(String path) {
        ArrayList<String> filepathlist = new ArrayList<>();
        try {
            File logFile = new File(path);
            if (logFile != null) {
                File[] files = logFile.listFiles();
                if (files != null) {
                    for (File f : files) {
                        if (f.isDirectory()) {
                            LogLocationUtils.writeLog(TAG, "Dir: " + f.getAbsoluteFile());
                        } else {
                            LogLocationUtils.writeLog(TAG, "File: " + f.getAbsoluteFile());
                            if (f.getAbsoluteFile() != null) {
                                filepathlist.add(f.getAbsolutePath());
                            }
                        }
                    }
                } else {
                    LogLocationUtils.writeLog(TAG, "--系统日志文件夹为空--");
                }
            } else {
                LogLocationUtils.writeLog(TAG, "--系统日志文件为null--");
            }
        } catch (Exception e) {
            LogLocationUtils.writeLog(TAG, "获取系统日志文件异常--" + e.getMessage());
        }
        return filepathlist;
    }


    /**
     * 获取当天
     *
     * @return
     */
    public static String getDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");//240417-164540
        dateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT+8")); // 设置时区为北京时间
        Date currentDate = new Date(System.currentTimeMillis());
        // 获取当前日期时间
        String dateTime = dateFormat.format(currentDate);
        return dateTime;
    }

    /**
     * 将日期转换为时间戳
     */
    public static long dateToTimestamp(String dateStr, String formatStr) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatStr);
            Date date = format.parse(dateStr);
            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 截取文件名称内的时间
     */
    public static String getFileTime(String filname) {
        // 假设文件名的格式为 "location_YYMMDD-HHMMSS"
        String pattern = "location_(\\d{6})-(\\d{6})";

        java.util.regex.Pattern r = java.util.regex.Pattern.compile(pattern);
        java.util.regex.Matcher m = r.matcher(filname);

        if (m.find()) {
            String dateString = "20" + m.group(1).substring(0, 2) + "-" +
                    m.group(1).substring(2, 4) + "-" +
                    m.group(1).substring(4, 6);
            String timeString = m.group(2).substring(0, 2) + ":" +
                    m.group(2).substring(2, 4) + ":" +
                    m.group(2).substring(4, 6);
            return dateString + " " + timeString;
        } else {
            return null;
        }
    }

    /**
     * 对比比较两个时间
     */
    public static boolean isTime(String Liptime, String locationtime) {
        int i = 0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date1 = dateFormat.parse(Liptime);
            Date date2 = dateFormat.parse(locationtime);

            // 1 结束时间小于开始时间 2 开始时间与结束时间相同 3 结束时间大于开始时间

            if (date1.getTime() > date2.getTime()) {
                i = 1;
            } else if (date1.getTime() == date2.getTime()) {
                i = 2;
            } else if (date1.getTime() < date2.getTime()) {
                //正常情况下的逻辑操作.
                i = 3;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (i == 1 || i == 2) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 截取RKINF内的时间
     */
    public static Date getRKINFDdate(String[] location) {
        int Num = 8;
        String day = "20" + location[1].substring(4, 6) + "-" + location[1].substring(2, 4) + "-" + location[1].substring(0, 2) + " " + location[2].substring(0, 2) + ":" + location[2].substring(2, 4) + ":" + location[2].substring(4, 6) + ":" + location[2].substring(7, 9);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date nowDate = null;
        try {
            nowDate = df.parse(day);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //如果需要向后计算日期 -改为+
        Date newDate2 = new Date(nowDate.getTime() + (long) Num * 60 * 60 * 1000);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateOk = simpleDateFormat.format(newDate2);
        Date date1 = null;
        try {
            date1 = simpleDateFormat.parse(dateOk);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date1;
    }

}
